
define(['bootstrapTable','bootstrapTableTreegrid','jqueryTreegrid','layer','iCheck'],function(bootstrapTable,bootstrapTreegrid,jqueryTreegrid,layer,iCheck){
    var pid = 0 ;
    var index =null;
    return{
		init:function(){
            getTreeTable();
            selectIc();
            $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
                 checkboxClass: 'icheckbox_flat-green',
                 radioClass   : 'iradio_flat-green'
            })
            /*关闭*/
            $(".btn_close").click(function(){
                $("#myRolesModal").css({"display":"none"})
                layer.closeAll();
            });
            /*添加按钮*/
            $(".addBtn").click(function(){
                $("#id").val(0);
                $("#parentMenu").val("");
                $("#menuName").val("");
                $("#menuAddress").val("");
                $("#menuSort").val("");
                $("#iconurl").val("");
                $("#getIcon").html("");
                $("#menuGrade").val("-1");
                var acheck =  $("#bootstrapTree").bootstrapTable('getSelections')[0];
                if(typeof(acheck) != 'undefined') {
                    $("#parentMenu").val(acheck.title);
                    pid = acheck.id;
                }else{
                    $("#parentMenu").val("无父级");
                }
                submitDictionary();
            });
            /*保存*/
            $("#btn_save").click(function(){
                var id =  $("#id").val();
                console.log($('input:radio[name="optionsRadiosinline"]:checked').val())
                var code = $('input:radio[name="optionsRadiosinline"]:checked').val();
                if(id == 0) {
                    $.ajax({
                        url: "/menuController/add",
                        dataType: "json",
                        type: "post",
                        data: {
                            "title": $("#menuName").val(),
                            "url": $("#menuAddress").val(),
                            "sort": $("#menuSort").val(),
                            "iconurl": $("#iconurl").val(),
                            "type": code,
                            "level": $("#menuGrade").val(),
                            "pid": pid,
                        },
                        success: function (result) {
                            if (result.code == 10001) {
                                layer.msg("保存成功！", {icon: 1});
                                layer.closeAll();
                                $('#bootstrapTree').bootstrapTable("refresh");
                            } else {
                                layer.msg("保存失败！", {icon: 1});
                            }
                        }
                    })
                }else{
                    $.ajax({// 修改
                        url: "/menuController/update",
                        dataType: "json",
                        type: "post",
                        data: {
                            "id": $("#id").val(),
                            "title": $("#menuName").val(),
                            "url": $("#menuAddress").val(),
                            "sort":$("#menuSort").val(),
                            "iconurl": $("#iconurl").val(),
                            "type":code,
                            "level": $("#menuGrade").val(),
                        },
                        success: function (result) {
                            layer.msg('修改成功', {icon: 1})
                            layer.closeAll();
                            $('#bootstrapTree').bootstrapTable("refresh");
                        }
                    })
                }
            });
            $("#icon").click(function(){
                $.ajax({
                    type : "post",
                    dataType:'json',
                    url:"/sysIconController/findAll",
                    async:false,
                    success:function(data){
                        var html ="";
                        for(var i=0;i<data.length;i++){

                            html+="<a data-icon='"+data[i].name+"' class='col-xs-11' style='display:inline-block;width:250px;line-height:40px;padding-left:3px; cursor:pointer;text-align:left'><i  style='width:20px; height:20px; font-size:25px; margin-right:10px; ' class='fa "+data[i].name+"' aria-hidden='true'></i> <span class='sr-only'>Example of </span>"+data[i].name+" <span class='text-muted'></span></a>"

                        }
                        $("#icon_form").html(html);
                    }

                });
                index = layer.open({
                    title:'选择菜单图标',
                    type: 1,
                    area: ['800px', '600px'],//宽高
                    content:$("#icon_form"),
                    end: function () {

                    }
                });
            });
            //清除图标
            $("#clearIcon").click(function(){
                $("#iconurl").val("");
                $("#getIcon").html("");
            });
		}
	};
    /**
     *  添加
     */
    function submitDictionary(){
        openLayer("添加数据字典",$("#editMenu"));
        //添加输入框验证
    }
    function openLayer(title,$dom){
        layer.open({
            type: 1,
            title:title,
            anim: 2,
            closeBtn:1,
            area:["60%","80%"],
            content:$dom,
            cancel: function(index, layero){
                $dom.css({"display":"none"})
            }
        });
    }
	function getTreeTable(){
	    window.openEvent={
            'click #editor':function (e,value,row,index) {
                openLayer("修改菜单",$("#editMenu"));
                console.log("打印【表情】值"+row.id);
                $("#id").val(row.id);
                $("#menuName").val(row.title);
                $("#menuAddress").val(row.url);
                $("#menuSort").val(row.sort);
                $("#iconurl").val(row.iconurl);
                $("input[name=optionsRadiosinline][value='0']").attr("checked", row.type == 0 ? true : false);
                $("input[name=optionsRadiosinline][value='1']").attr("checked", row.type == 1 ? true : false);
                $("#menuGrade").val(row.level);
                if(row.pname.length==0){
                    $("#parentMenu").val("无父级");
                }else{
                    $("#parentMenu").val(row.pname);
                }
            },
            'click #delete':function (e,value,row,index) {
                layer.confirm('您确定要删除此菜单？', {title:'删除菜单信息',icon: 2,
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"/menuController/del",
                        dataType:"json",
                        type:"post",
                        data:{
                            "id":row.id
                        },
                        success:function(result){
                            if(result.code==10001){
                                layer.msg("删除成功！",{icon:1});
                                layer.closeAll();
                                $('#bootstrapTree').bootstrapTable("refresh");
                            }else{
                                layer.msg("删除失败！",{icon:1});
                            }
                        }
                    })
                    layer.close();
                });
            }
        }
        $("#bootstrapTree").bootstrapTable({
            url:'/menuController/searchList',
            method:'post',
            striped:true,
            sidePagenation:'server',
            idField:'id',
            cache: false,       // 设置为 false 禁用 AJAX 数据缓存， 默认为true
            contentType:'application/x-www-form-urlencoded',
            striped: true,      //表格显示条纹，默认为false
            pagination: true,   // 在表格底部显示分页组件，默认false
            pageList: [20,50,100], // 设置页面可以显示的数据条数
            pageSize: 20,       // 页面数据条数
            pageNumber: 1,      // 首页页码
            //showRefresh: true,  //显示刷新按钮
            //search: true,//是否显示右上角的搜索框
            //searchAlign:'left',
            columns:[
                {
                    field: 'ck',
                    checkbox: true
                },{
                    field:'title',
                    align: 'center',
                    valign: 'middle',
                    title:'菜单名称'
                },{
                     field:'url',
                     align: 'center',
                     valign: 'middle',
                     title:'菜单地址'
                },{
                     field:'level',
                     align: 'center',
                     valign: 'middle',
                     title:'菜单等级'
                },{
                     field:'type',
                     align: 'center',
                     valign: 'middle',
                     title:'是否为按钮',
                    formatter:function(value,row,index){
                        if(value==1){
                            return "否";
                        }
                        if(value==0){
                            return "是";
                        }
                    }
                },{
                     field:'sort',
                     align: 'center',
                     valign: 'middle',
                     title:'排序'
                },{
                    title:'图标',
                    field:'iconurl',
                    align:'center',
                    width:100,
                    formatter:function(value,row,index){
                        return "<a class='col-xs-11' style='margin-right:20px;'><i  style='width:20px; height:20px; font-size:25px; margin-right:10px; ' class='fa "+row.iconurl+"' aria-hidden='true'></i></a>";
                    }
                },{
                     field:'createUser',
                     align: 'center',
                     valign: 'middle',
                     title:'创建人'
                },{
                     field:'createTime',
                     align: 'center',
                     valign: 'middle',
                     title:'创建时间'
                },{
                     field:'operator',
                     align: 'center',
                     valign: 'middle',
                     title:'操作',
                     events:openEvent,
                     formatter:function(value,row,index){
                        return ['<button class="btn btn-xs btn-info btn-operator-gew" id="editor"><i class="fa fa-edit"></i></button>','<button class="btn btn-xs btn-danger" id="delete"><i class="fa fa-trash-o"></i></button>'].join("");
                     }
                }
            ],
            treeShowField: 'title',
            parentIdField: 'pid',
            onLoadSuccess: function(data) {
                console.log(data)
                $("#bootstrapTree").treegrid({
                    initialState: 'collapsed',//收缩
                    treeColumn: 1,//指明第几列数据改为树形
                    expanderExpandedClass: 'fa fa-chevron-circle-down',
                    expanderCollapsedClass: 'fa fa-chevron-circle-right',
                    onChange: function() {
                        $("#bootstrapTree").bootstrapTable('resetWidth');
                    }
                });
            }
        });

    };
    //获取选择图标
    function selectIc(name){
        $("#icon_form").on('click','a',function(){
            var iconName =$(this).data('icon')
            console.log("****"+iconName)
            var html ="<a><i  style='font-size:25px;' class='fa "+iconName+"' aria-hidden='true'></i></a>"
            $("#getIcon").html(html);
            $("#iconurl").val(iconName);
            layer.close(index);
            $("#icon_form").hide()
        })
    }

});